
var flag = 0;

function GetQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);//search,查询？后面的参数，并匹配正则
    if (r != null) return unescape(r[2]);
    return null;
}



//获取表名
var Reporttablename = GetQueryString("tablename");

//获取与表名对应的所有报告


//加载版本号
function AddVersion() {
    $.ajax({
        url: "/gray/web/language/english/properties/FindAllVersion",
        data: {tablename: Reporttablename},
        type: "Post",
        //async:falase,
        success: function (data) {
            if (data.code != null) {
                var code = data.code;
                var message = data.message;
                if (code == "400") {
                    alert(message);
                    window.parent.location.href = "/login.html";
                }
                else if (code == "401") {
                    alert(message);
                    window.location.href = "/error/error.html";
                }
                return;
            }
            var items = "";
            var item = "";
            $.each(data, function (i, result) {
                item = "<option value='\"" + result + "\"'>" + result + "</option>";
                items += item;
            })
            items += "<option selected='selected' value=''>所有版本</option>";
            $('#version_select').html(items);
            search();
        }

    });
}

//按日期查找
function search() {
    flag = 1;
    document.getElementById("box_center").style.display = "block";
    document.getElementById("highchart").style.display = "block";
    document.getElementById("box_bottom2").style.display = "none";
    document.getElementById("box_bottom").style.display = "block";
    var version = document.getElementById("version_select").value;
    var allsuccesssum = 0;
    var allcasenum = 0;
    var start = document.getElementById("start_time").value;
    var end = document.getElementById("end_time").value;
    if (start == "" || end == "") return;
    var tablename = Reporttablename;
    var casetablename = "";
    switch (tablename) {
        case 'gray_web_language_english_properties_report':
            casetablename = "gray_web_language_english_properties_case";
            break;
    }
    $.ajaxSettings.async = false;
    var countnum = 0;
    $("#show_table").bootstrapTable('destroy');
    $('#show_table').bootstrapTable({
        url: "/gray/web/language/english/properties/FindAllReportByTime",
        method: 'post',
        contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
        cache: false,
        queryParams: queryParams,
        striped: true,
        pagination: true,
        sortable: true,
        // sortOrder: "asc",//升序
        sortOrder: "desc",//降序
        sortName: 'reporttime',
        sidePagination: 'client',
        pageSize: 10,
        pageNumber: 1,
        pageList: [10, 20, 50, 100, 200, 500],
        search: true,
        strictSearch: false,
        showColumns: true,
        showRefresh: false,
        showExport: false,
        uniqueId: "rid",
        showToggle: false,
        cardView: false,
        // clickToSelect: true,
        showFooter: true,
        columns:
            [
                // {field: "checked", checkbox: true},
                {field: "rid", title: "编号", align: "center", valign: "middle", sortable: "true"},
                {field: "type", title: "类型", align: "center", valign: "middle", sortable: "true"},
                {field: "version", title: "版本", align: "center", valign: "middle", sortable: "true"},
                {
                    field: "jobname",
                    title: "名称",
                    align: "center",
                    valign: "middle",
                    sortable: "true",
                    formatter: Addbutton
                },
                {
                    field: "casenum",
                    title: "总数",
                    align: "center",
                    valign: "middle",
                    sortable: "true",
                    footerFormatter: Conutnum
                },
                {field: "reporttime", title: "时间", align: "center", valign: "middle", sortable: "true"},
            ],

    });

    function Conutnum(value) {
        var count = 0;
        var successconut = 0;
        console.log(value);
        for (var i = 0; i < value.length; i++) {
            count += parseFloat(JSON.parse(JSON.stringify(value[i])).casenum);
            successconut += parseFloat(JSON.parse(JSON.stringify(value[i])).successnum);
        }
        allcasenum = count;
        allsuccesssum = successconut;
        return "Total:" + count + ",Success:" + successconut;
    }


    function Addbutton(value, row, index) {
        //   alert(row.id+"索引"+index);
        return [
            '<button class="btn btn-default" style="width: 100%;background-color: #9fcdff"  onclick="FindBrowserByRid(\'' + row.rid + '\',\'' + casetablename + '\')">' + value + '</button>'
        ].join("")
    }

    function queryParams(params) {
        return {
            tablename: tablename,
            start: start,
            end: end,
            version: version
        }
    }
}

//查询每条报告对应的CASE
var temprid, temptablename;//记录用于返回
function FindBrowserByRid(rid, tablename) {
    var Browser = [[]];
    flag = 2;
    temprid = rid;
    temptablename = tablename;
    document.getElementById("box_center").style.display = "none";
    document.getElementById("highchart").style.display = "block";
    document.getElementById("box_bottom2").style.display = "none";
    document.getElementById("box_bottom").style.display = "block";
    $.ajaxSettings.async = false;
    var successnum = 0;
    var countnum = 0;
    $("#show_table").bootstrapTable('destroy');
    $('#show_table').bootstrapTable({
        url: "/gray/web/language/english/properties/FindByRid",
        method: 'post',
        contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
        cache: false,
        queryParams: queryParams,
        striped: true,
        pagination: true,
        sortable: true,
        sortOrder: "asc",
        sortName: 'type',
        sidePagination: 'client',
        pageSize: 10,
        pageNumber: 1,
        pageList: [10, 20, 50, 100, 200, 500],
        search: true,
        strictSearch: false,
        showColumns: true,
        showRefresh: false,
        showExport: false,
        // uniqueId: "id",
        showToggle: false,
        cardView: false,
        // clickToSelect: true,
        showFooter: true,
        columns:
            [
                //  {field: "checked", checkbox: true},
                {
                    field: "number",
                    align: "center",
                    valign: "middle",
                    title: "序号",
                    formatter: function (value, row, index) {
                        return index + 1;
                    }
                },
                {field: "linenum", title: "行号", align: "center", valign: "middle", sortable: "true"},
                {field: "text", title: "配置信息", align: "center", valign: "middle", sortable: "true"},
                {
                    field: "illchar",
                    title: "非法字符",
                    align: "center",
                    valign: "middle",
                    sortable: "true",
                    cellStyle: function (value, row, index) {
                        return {css: {"color": "red"}}

                    }
                },


            ],
    });


    function queryParams(params) {
        return {
            rid: rid,
            tablename: tablename
        }
    }

}



//返回上一级
function returnLastPage() {
    switch (flag) {
        case 0:
            break;
        case 1:
            break;
        case 2:
            search();
            break;
        case 3:
            FindBrowserByRid(temprid, temptablename);
            break;
        case 4:
            FindCaseByTypeAndRid(temprid, temptype, temptablename);
            break;
    }
}

